package com.sskj.level.presenter;

import android.annotation.SuppressLint;

import com.google.gson.Gson;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.level.bean.CoinUpdateBean;
import com.sskj.level.bean.ProductDataBean;
import com.sskj.level.bean.TradeInfoBean;
import com.sskj.level.http.HttpConfig;
import com.sskj.level.ui.fragment.BuyAndSellFragment;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.http.CallBackOption;
import com.sskj.lib.http.JsonConverter;
import com.sskj.lib.util.CommonUtil;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class BuyAndSellFragmentPresenter extends BasePresenter<BuyAndSellFragment> {

    private WebSocketObserver stockSocket;
    private Disposable stockDisposable;
    private String code;

    public void getBalance(UserData userData) {
        if (userData == null) {
            return;
        }
        httpService.getTradeInfo()
                .execute(new CallBackOption<HttpData<TradeInfoBean>>() {
                }
                        .loadBind(mView)
                        .execute(httpData -> {
                            if (httpData.getStatus() == HttpConfig.OK) {
                                mView.updateRisk(httpData.getData());
                            }
                        }));
    }

    @SuppressLint("CheckResult")
    public void getCoinInfo(String code) {
        Flowable<CoinBean> coinFlow = httpService.getPro(code)
                .converter(new JsonConverter<HttpData<List<CoinBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(listHttpData -> listHttpData.getData().get(0));
        Flowable<ProductDataBean> setFlow = httpService.getProductData(code)
                .converter(new JsonConverter<HttpData<ProductDataBean>>() {
                })
                .adapt(new FlowableBody<>())
                .map(HttpData::getData);
        Flowable.zip(coinFlow, setFlow, (coinBean, productDataBean) -> {
            CoinUpdateBean coinUpdateBean = new CoinUpdateBean();
            coinUpdateBean.setBuyPrice(coinBean.getBuy());
            coinUpdateBean.setSellPrice(coinBean.getSell());
            coinUpdateBean.setMinDealNum(productDataBean.getContractMin());
            coinUpdateBean.setSpread(productDataBean.getSpread());
            coinUpdateBean.setSlidingScalePrice(productDataBean.getSlidingScalePrice());
            coinUpdateBean.setShouPer(productDataBean.getLeverDealFee());
            coinUpdateBean.setPrice(coinBean.getPrice());
            coinUpdateBean.setCode(coinBean.getCode());
            coinUpdateBean.setLevels(productDataBean.getStockLever());
            coinUpdateBean.setPaperValue(productDataBean.getLotSize());
            mView.updateCoinData(coinUpdateBean);
            return coinUpdateBean;
        }).subscribe(coinUpdateBean -> mView.updateCoinData(coinUpdateBean), e -> {
            System.out.println("12");
        });
    }

    public void createOrder(String entrustLot, String buyPrice, String stockCode, String billPriceType,
                            String buyBillType, String stopProfit, String stopLoss, String lever) {
        httpService.createOrder(entrustLot, buyPrice, buyBillType, stockCode, billPriceType, stopProfit, stopLoss, lever)
                .execute(new CallBackOption<HttpData>() {
                }
                        .loadBind(mView)
                        .execute(httpData -> {
                            if (httpData.getStatus() == HttpConfig.OK) {
                                mView.buySuccess();
                            }
                        }));
    }

    public void initSocket(String code) {
        if (code == null)
            return;
        code = CommonUtil.dealReuqestCode(code);
        if (stockSocket == null) {
            stockSocket = httpService.pushCoinFiveDeep(code);
            stockDisposable = stockSocket
                    .map(s -> new Gson().fromJson(s, WSFiveBean.class))
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(data -> mView.updateDeep(data), e -> {

                    });
        } else {
            stockSocket.send("{\"type\":\"depth_list\",\"list\":[\"" + code + "\"]}");
        }
        this.code = code;
    }

    public void getDeepData(String code) {
        httpService.getDeepData(code)
                .execute(new CallBackOption<HttpData<WSFiveBean>>() {
                }
                        .unLoadBind(mView)
                        .execute(httpData -> {
                            if (httpData.getStatus() == HttpConfig.OK && httpData.getData() != null) {
                                mView.updateDeep(httpData.getData());
                            }
                        }));
    }


}
